package com.tucc.hello.leetcode.no1_400.no1_200.no1_20.no11_20.no15;

import cn.hutool.core.io.FileUtil;

import java.util.*;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
//        int[] nums = new int[]{-1, 0, 1, 2, -1, -4};
        int[] nums = new int[]{0, 0, 0, 1, -1};
        String path = "C:\\Users\\tucc38782\\Desktop\\test.txt";
        String text = FileUtil.readUtf8String(path).trim();
        text = text.substring(1, text.length() - 1);
        String[] parts = text.split(",");
        nums = new int[parts.length];
        for(int i = 0;i < parts.length;i ++){
            nums[i] = Integer.parseInt(parts[i]);
        }
        List<List<Integer>> resultList = solution.threeSum(nums);
        resultList.forEach((list) -> {
            list.forEach((num) -> {
                System.out.print(num + " ");
            });
            System.out.println();
        });
    }

    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> resultList = new ArrayList<>();
        for (int i = 0; i < nums.length && nums[i] <= 0; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int k = i + 2;
            for (int j = i + 1; j < nums.length - 1 && nums[i] + nums[j] <= 0; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) {
                    continue;
                }
                int curSum = nums[i] + nums[j];
                if (k <= j) {
                    k = j + 1;
                }
                if (k >= nums.length) {
                    k = nums.length - 1;
                }
                if (k > j && k < nums.length) {
                    if (curSum + nums[k] == 0) {
                        addToResult(resultList, nums, i, j, k);
                    } else if (curSum + nums[k] < 0) {
                        while (++k < nums.length) {
                            if (curSum + nums[k] > 0) {
                                break;
                            } else if (curSum + nums[k] == 0) {
                                addToResult(resultList, nums, i, j, k);
                                break;
                            }
                        }
                    } else {
                        while (--k > j) {
                            if (curSum + nums[k] < 0) {
                                break;
                            } else if (curSum + nums[k] == 0) {
                                addToResult(resultList, nums, i, j, k);
                                break;
                            }
                        }
                    }
                }
            }
        }
        return resultList;
    }

    public void addToResult(List<List<Integer>> resultList, int[] nums, int i, int j, int k) {
        List<Integer> result = new ArrayList<>();
        result.addAll(Arrays.asList(nums[i], nums[j], nums[k]));
        resultList.add(result);
    }
}
